package com.goddess.yeecolor.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.goddess.yeecolor.Modle.NewsModle;
import com.goddess.yeecolor.R;

import java.util.List;

/**
 * 作者: CaoJunping
 * 时间: 2017/1/13
 * 作用: ******
 * 备注: 消息页面Adapter
 */
public class NewsActivityAdapter extends BaseRecyclerAdapter<NewsActivityAdapter.NewActivityViewHolder> {
    private Context context;
    private List<NewsModle> modles;

    public NewsActivityAdapter(Context context, List<NewsModle> modles) {
        this.context = context;
        this.modles = modles;
    }

    @Override
    public NewActivityViewHolder getViewHolder(View view) {
        return new NewActivityViewHolder(view);
    }

    @Override
    public NewActivityViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View view= LayoutInflater.from(context).inflate(R.layout.activity_news_recyclerview_item,parent,false);
        return new NewActivityViewHolder(view);
    }

    @Override
    public void onBindViewHolder(NewActivityViewHolder holder, int position, boolean isItem) {
        NewsModle modle =modles.get(position);
        holder.tv_title.setText(modle.getTitle());
        holder.tv_word.setText(modle.getWord());
        holder.tv_date.setText(modle.getDate());
        holder.img.setScaleType(ImageView.ScaleType.FIT_XY);
    }

    @Override
    public int getAdapterItemCount() {
        return modles.size();
    }

    class NewActivityViewHolder extends RecyclerView.ViewHolder{
        private ImageView img;
        private TextView tv_title,tv_word,tv_date;
        public NewActivityViewHolder(View itemView) {
            super(itemView);
            img= (ImageView) itemView.findViewById(R.id.img);
            tv_title= (TextView) itemView.findViewById(R.id.tv_title);
            tv_word= (TextView) itemView.findViewById(R.id.tv_word);
            tv_date= (TextView) itemView.findViewById(R.id.tv_date);

            /**
             * 单行点击事件
             */
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClicklistener.OnItemClick(v,getLayoutPosition()-1);
                }

            });
        }
    }

    /**
     * item点击事件
     */
    public  interface OnItemClicklistener{
        void OnItemClick(View view,int position);
    }
    private OnItemClicklistener onItemClicklistener;
    public void setOnItemClicklistener(OnItemClicklistener onItemClicklistener){
        this.onItemClicklistener=onItemClicklistener;
    }

    /**
     * 清空适配器
     */
    public void clear(){
        clear(modles);
    }

    /**
     * 添加数据
     */
    public void insert(NewsModle modle,int position){
        insert(modles,modle,position);
    }
}
